/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.uct.service.plugins  
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.uct.common.entity.ActGive;
import com.apache.uct.common.entity.DataRight;
import com.apache.uct.common.entity.Dept;
import com.apache.uct.common.entity.Org;
import com.apache.uct.common.entity.RoleGive;
import com.apache.uct.common.entity.User;
import com.apache.uct.manager.ActGiveManager;
import com.apache.uct.manager.ActManager;
import com.apache.uct.manager.DataRightManager;
import com.apache.uct.manager.DeptManager;
import com.apache.uct.manager.OrgManager;
import com.apache.uct.manager.RoleGiveManager;
import com.apache.uct.manager.RoleManager;
import com.apache.uct.manager.UserManager;

/**
 * description:  删除机构、部门等信息插件
 * @author dwusoft 创建时间：2015-3-12  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class DeleteOrgPlugins implements PluginConnector {

	private OrgManager orgManager;
	private UserManager userManager;
	private DeptManager deptManager;
	private ActGiveManager actGiveManager;
	private DataRightManager dataRightManager;
	private RoleManager roleManager;
	private ActManager actManager;
	private RoleGiveManager roleGiveManager;

	/**
	 * TODO 删除机构信息插件.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo vo) throws Exception {
		String result = "";
		String infoType = String.valueOf(vo.getParams("infoType"));
		if ("org".equals(infoType)) {
			/**
			 * 步骤:1、验证机构下是否有子机构、部门及用户； 2、删除机构绑定的权限；  3、删除机构信息;
			 */

			if (Validator.isNotNull(String.valueOf(vo.getParams("orgId")))) {
				String infoId = String.valueOf(vo.getParams("orgId"));
				vo.setInfoId(infoId);
			}

			result = checkDelInfo(vo.getInfoId());
		} else if ("dept".equals(infoType)) {
			/**
			 * 步骤:1、验证部门下是否有子部门及用户； 2、删除部门绑定的权限； 3、删除部门信息;
			 */
			result = checkDelDeptInfo(vo.getInfoId());
		}
		/**
		 * 删除用户步骤:1、删除用户与角色的绑定关系； 2、删除用户绑定的权限； 3、删除用户信息或修改用户信息为删除状态;
		
		 * 删除角色步骤:1、删除用户与角色的绑定关系； 2、删除角色绑定的权限； 3、删除角色信息;

		 * 删除权限步骤:1、删除权限绑定的关系； 2、删除权限信息;
		 */
		if (Validator.isNotNull(result))
			return result;
		result = doDelInfo(vo, infoType);
		if (Validator.isNotNull(result)) {
			return result;
		} else {
			ActCacheHelper.getInstance().delCache(vo.getInfoId(), infoType);//删除权限缓存
		}
		return "";
	}

	/**
	 * description:  验证机构下是否有子机构、部门及用户
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private String checkDelInfo(String orgId) {
		User user = new User();
		ParamsVo vo = new ParamsVo();
		user.setOrgId(orgId);
		vo.setObj(user);
		long usNum = userManager.countInfo(vo);
		if (usNum > 0)
			return "机构下存在用户,不能删除此机构";
		Dept dept = new Dept();
		dept.setOrgId(orgId);
		vo.setObj(dept);
		long dpNum = deptManager.countInfo(vo);
		if (dpNum > 0)
			return "机构下存在部门,不能删除此机构";
		Org org = new Org();
		org.setFatherId(orgId);
		vo.setObj(org);
		long orgNum = orgManager.countInfo(vo);
		if (orgNum > 0)
			return "机构下存在子机构,不能删除此机构";
		return "";
	}

	/**
	 * description:  删除绑定的权限
	 */
	private String doDelInfo(ParamsVo vo, String infoType) {
		ParamsVo param = new ParamsVo();
		ActGive ag = new ActGive();
		if ("act".equals(infoType)) {
			ag.setActId(vo.getInfoId());
		} else {
			ag.setRoleId(vo.getInfoId());
			ag.setGiveType(infoType);
		}
		param.setObj(ag);
		actGiveManager.deleteInfo(param);
		if ("org".equals(infoType) || "dept".equals(infoType) || "user".equals(infoType)) {//删除数据权限
			DataRight dr = new DataRight();
			dr.setDataName("org");
			dr.setDataKey(vo.getInfoId());
			param.setObj(dr);
			dataRightManager.deleteInfo(param);
		}
		if ("role".equals(infoType) || "user".equals(infoType)) {//删除角色用户关系
			RoleGive roleGive = new RoleGive();
			if ("role".equals(infoType)) {
				roleGive.setRoleId(vo.getInfoId());
			} else {
				roleGive.setUserId(vo.getInfoId());
			}
			param.setObj(roleGive);
			roleGiveManager.deleteInfo(param);
		}
		if ("1".equals(SystemTools.getInstance().getValue("org_act_switch"))) {
			if ("role".equals(infoType) || "org".equals(infoType)) {//删除角色机构关系
				ParamsVo paramv = new ParamsVo();
				paramv.setInfoId(vo.getInfoId());
				paramv.setKey(infoType);
				roleManager.deleteInfo(paramv);
			}
		}
		boolean mark = false;
		vo.setKey("");
		if ("org".equals(infoType)) {
			mark = orgManager.deleteInfo(vo);
		} else if ("dept".equals(infoType)) {
			mark = deptManager.deleteInfo(vo);
		} else if ("user".equals(infoType)) {
			mark = userManager.deleteInfo(vo);
		} else if ("act".equals(infoType)) {
			mark = actManager.deleteInfo(vo);
		} else if ("role".equals(infoType)) {
			mark = roleManager.deleteInfo(vo);
		}
		if (!mark)
			return "删除信息失败!";
		return "";
	}

	/**
	 * description:  验证部门下是否有子部门及用户
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private String checkDelDeptInfo(String deptId) {
		User user = new User();
		ParamsVo vo = new ParamsVo();
		user.setDeptId(deptId);
		vo.setObj(user);
		long usNum = userManager.countInfo(vo);
		if (usNum > 0)
			return "部门下存在用户,不能删除此部门";
		Dept dept = new Dept();
		dept.setFatherId(deptId);
		vo.setObj(dept);
		long dpNum = deptManager.countInfo(vo);
		if (dpNum > 0)
			return "部门下存在子机构,不能删除此部门";
		return "";
	}

	public void setOrgManager(OrgManager orgManager) {
		this.orgManager = orgManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setDeptManager(DeptManager deptManager) {
		this.deptManager = deptManager;
	}

	public void setActGiveManager(ActGiveManager actGiveManager) {
		this.actGiveManager = actGiveManager;
	}

	public void setDataRightManager(DataRightManager dataRightManager) {
		this.dataRightManager = dataRightManager;
	}

	public void setRoleManager(RoleManager roleManager) {
		this.roleManager = roleManager;
	}

	public void setActManager(ActManager actManager) {
		this.actManager = actManager;
	}

	public void setRoleGiveManager(RoleGiveManager roleGiveManager) {
		this.roleGiveManager = roleGiveManager;
	}

}
